package com.example.musicplayer

import android.annotation.SuppressLint
import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.Binder
import android.os.IBinder
import android.provider.MediaStore
import android.util.Log
import java.io.IOException

class MusicService : Service() {
    companion object{
        val Commoned="Operate"
    }
    val TAG = "MusicService"
    val mediaPlayer = MediaPlayer()
    val musicList: MutableList<String> = mutableListOf<String>()
    val musicNameList: MutableList<String> = mutableListOf<String>()
    var current = 0
    var isPause=false
    val binder = MusicBinder()


    inner class MusicBinder : Binder() {
        val musicName: String
            get() = musicNameList.get(current)
        var currentPosition: Int
            get() = mediaPlayer.currentPosition
            set(value:Int)=mediaPlayer.seekTo(value)

        val duration: Int
            get() = mediaPlayer.duration

        val size: Int
            get() = musicList.size

        val currentIndex: Int
            get() = current

    }
    override fun onCreate() {
        super.onCreate()
        getMusicList()
        mediaPlayer.setOnPreparedListener{
            it.start()
        }
        mediaPlayer.setOnCompletionListener{
            current ++
            if (current>=musicList.size){
                current=0
            }
            play()
        }
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

        val operate:Int=intent?.getIntExtra(Commoned,1)?:1
        when(operate){
            1 -> play()
            2 -> pause()
            3 -> stop()
            4 -> next()
            5 -> previous()
        }
        return super.onStartCommand(intent, flags, startId)
    }
    fun pause() {
        if (isPause){
            mediaPlayer.start()
            isPause=false
        }else{
            mediaPlayer.pause()
            isPause=true
        }
    }

    fun next() {
        current++
        if (current>=musicList.size){
            current=0
        }
        play()
    }

    fun stop() {
        mediaPlayer.stop()
        stopSelf()
    }

    fun previous() {
        current --
        if (current<0){
            current=musicList.size-1
        }
        play()
    }
    fun play() {
        if (musicList.size==0) return
        val path: String = musicList[current]
        mediaPlayer.reset()
        try {
            mediaPlayer.setDataSource(path)
            mediaPlayer.prepareAsync()
        }catch (e: IOException){
            e.printStackTrace()
        }
    }
    override fun onBind(intent: Intent): IBinder {

        return binder
    }
    @SuppressLint("Range")
    private fun getMusicList(){
        val cursor = contentResolver.query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            null,
            null,
            null,
            null
        )
        if (cursor != null) {
            while (cursor.moveToNext()) {
                val musicName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))
                musicNameList.add(musicName)
                val musicPath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA))
                musicList.add(musicPath)
                Log.d(TAG, "getMusicList: $musicPath name: $musicName")
            }
            cursor.close()
        }
    }
}